package lumis.portal.serialization.persistency.sub;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import lumis.portal.serialization.persistency.IDirectory;

/**
 * {@link IDirectory} implementation that represents a subdirectory of another {@link IDirectory}.
 *
 * @version $Revision: 6376 $ $Date: 2007-04-10 17:55:53 -0300 (Tue, 10 Apr 2007) $
 * @since 4.0.10
 */
public class SubDirectory implements IDirectory
{
	private final IDirectory directory;
	private final String path;
	
	public SubDirectory(final IDirectory directory, final String path)
	{
		super();
		this.directory = directory;
		this.path = path.endsWith("/") ? path.substring(0, path.length() - 1) : path;
	}

	public InputStream getInputStream(String filePath) throws FileNotFoundException, IOException
	{
		return directory.getInputStream(path + (filePath.startsWith("/") ? "" : "/") + filePath);
	}

	public OutputStream getOutputStream(String filePath) throws IOException
	{
		return directory.getOutputStream(path + (filePath.startsWith("/") ? "" : "/") + filePath);
	}

	public void close() throws IOException
	{
		directory.close();
	}

	public boolean exists(String filePath) throws IOException
	{
		return directory.exists(path + (filePath.startsWith("/") ? "" : "/") + filePath);
	}

	public String getDescription()
	{

		return this.getClass().getName() + " path=" + path + " baseDirectory=( " + directory.getDescription() + " )";
	}

	public void delete() throws IOException
	{
		throw new UnsupportedOperationException("A SubDirectory has no support for deletion.");
	}
}
